﻿/// <reference path="../../../../SIR.Web/scripts/_references.js" />
/// <reference path="../../../../SIR.Web/scripts/jquery-1.9.1.js" />
/// <reference path="../../../../SIR.Web/scripts/angular.js" />
/// <reference path="../../../../SIR.Web/scripts/angular-mocks.js" />

/// <reference path="../../../../SIR.Web/app/app.js" />
/// <reference path="../../../../SIR.Web/app/common/common.js" />
/// <reference path="../../../../SIR.Web.Tests/JavaScript/sir-mock.js" />
/// <reference path="../../../../SIR.Web/app/authenticate/authenticate.js" />
/// <reference path="../../../../SIR.Web/app/authenticate/services/auth-service.js" />
/// <reference path="../../../../SIR.Web/app/authenticate/controllers/user-control-panel-controller.js" />

describe('User Control Panel Controller - Log In:', function () { 
    'use strict';
	// initialize variables and modules
    var scope, location, controller, rootScope, $httpBackend, notifyService;

    beforeEach(module('ui.router'));
    beforeEach(module('app.common'));
    beforeEach(module('app.authenticate'));

    beforeEach(inject(function ($injector, $controller, $rootScope, authService, notify) {
		// set variables for each test
    	notifyService = notify;
    	$httpBackend = $injector.get('$httpBackend');
    	rootScope = $rootScope;
    	scope = $rootScope.$new();
		// expect log in
    	$httpBackend.expectPOST('/api/Authentication/LogIn').respond({ Successful: false, Content: null, Message: '' });
    	controller = $controller('ControlPanelController', { $scope: scope, $state: {}, authService: authService });
    	$httpBackend.flush();
		// spy on the handler and the warning messages
    	spyOn(controller, 'handleLogInReply').andCallThrough();
    	spyOn(notify, 'warning').andCallThrough();
    }));

	// expect all requests to have been handled
    afterEach(function () {
    	$httpBackend.verifyNoOutstandingExpectation();
    	$httpBackend.verifyNoOutstandingRequest();
    });

    describe('Controller - User Logout (Total: 1)', function () {
    	it('[1] Produce \'Log Out Successful\' message when user logs out', function () {
			// attempt successful log ing
    		spyOn(controller, 'showLoggedInState');
    		$httpBackend.expectPOST('/api/Authentication/LogIn').respond({ Successful: true, Content: {}, Message: 'Authentication Successful' });
    		scope.model.email = 'email';
    		scope.model.password = 'password';
    		scope.logIn();
    		scope.$digest();
    		$httpBackend.flush();
			// expect that the log in was successful and in a logged in state
    		expect(controller.handleLogInReply).toHaveBeenCalled();
    		expect(controller.showLoggedInState).toHaveBeenCalled();
    		expect(scope.user).toNotBe(null);
    		expect(scope.user).toNotBe(undefined);
    		expect(scope.model.email).toBe('');
    		expect(scope.model.password).toBe('');
			// expect the log out and return a successful result
    		$httpBackend.expectPOST('/api/Authentication/LogOut').respond({ Successful: true, Content: null, Message: 'Logged Out' });
			// log out
    		scope.logOut();
    		$httpBackend.flush();
			// expect the warning was given to the user
    		expect(notifyService.warning).toHaveBeenCalledWith('Logged Out');
			// expect the user to be null and no longer logged in
    		expect(scope.user).toBe(null);
    	});
    });
});